package com.tokaplot;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;

public class DialogTime extends Dialog {
    
    // Currently selected time
    long dTime = 60000;
	
    public interface timeDoneListener {   	 
        public void done(long tMilli);
    }
    private timeDoneListener dListener;
    
	public DialogTime(Context context, long time, timeDoneListener listener) {
		super(context);
		
		// Time picker dialog
		dTime = time;
		dListener = listener;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
	    
		requestWindowFeature(Window.FEATURE_LEFT_ICON);
		setContentView(R.layout.dialog_time);
        setTitle("Set time");
        setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.icon);
        update();
        
        // Done button listener for Add dialog
        findViewById(R.id.dtime_hours_up).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) { dTime += 3600000; update(); }
        });
        findViewById(R.id.dtime_hours_down).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) { dTime -= 3600000; update(); }
        });
        findViewById(R.id.dtime_mins_up).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) { dTime += 60000; update(); }
        });
        findViewById(R.id.dtime_mins_down).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) { dTime -= 60000; update(); }
        });
        findViewById(R.id.dtime_secs_up).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) { dTime += 1000; update(); }
        });
        findViewById(R.id.dtime_secs_down).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) { dTime -= 1000; update(); }
        });
        
		// Done button listener for Add dialog
		findViewById(R.id.dtime_done).setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            	
            	// Call listener
                dListener.done(dTime);
            	dismiss();
            }
        });
		
		// Cancel button listener for Add dialog
		findViewById(R.id.dtime_cancel).setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				dismiss();
			}
		});

	}
	
	public void update() {
	    
	    // Check time
	    if (dTime < 1000) dTime = 1000;
	    
        // Split time
        long ttime = dTime / 1000;
        long thrs = ttime / 3600;
        long tmin = (ttime % 3600) / 60;
        long tsec = ttime % 60;
        
        // Set to current time
        ((EditText)findViewById(R.id.dtime_hours)).setText(Long.toString(thrs));
        ((EditText)findViewById(R.id.dtime_mins)).setText(Long.toString(tmin));
        ((EditText)findViewById(R.id.dtime_secs)).setText(Long.toString(tsec));
	}

}
